#include "Ant.h"
#include "Environment.h"

Ant *queen = NULL;

Ant::Ant(EcoSystem *eco_system) :
Consumer(eco_system)
{
    if (queen == NULL)
    {
        is_this_queen = true;
        queen = this;
    }
    else
    {
        is_this_queen = false;
    }
    is_carrying_grass = is_going_back_to_nest = is_going_to_last_position_found_grass = false;
    set_max_hp(1000.0);
    set_hp(100.0);
}
Ant::~Ant()
{
}

inline const unsigned int Ant::get_species_id() const
{
    return ANT_SPECIES_ID;
}
Gdiplus::Image *Ant::get_entity_image() const
{
    static Gdiplus::Image *ant = Gdiplus::Image::FromFile(L".\\images\\ant.png");
    static Gdiplus::Image *queen_ant = Gdiplus::Image::FromFile(L".\\images\\queenant.png");
    static Gdiplus::Image *antcarryinggrass = Gdiplus::Image::FromFile(L".\\images\\antcarryinggrass.png");

    if (is_this_queen)
        return queen_ant;
    if (is_carrying_grass)
        return antcarryinggrass;
    return ant;
}

void Ant::set_dead()
{
    if (is_this_queen)
        queen = NULL;
    Entity::set_dead();
}
int Ant::get_cost_of_being_preyed() const
{
    return 1;
}
int    Ant::get_gain_after_being_preyed() const
{
    return 1;
}
Entity *Ant::new_entity() const
{
    return new Ant(eco_system);
}
void Ant::on_tick()
{
    set_age(get_age() + 1);

    if (is_this_queen)
    {
        if (eco_system->get_environment_instance()->get_rainfall() > 400.0)
            set_dead();
    }
    else
    {
        if (is_going_back_to_nest)
        {
            if (go_back_to_nest())
            {
                is_carrying_grass = is_going_back_to_nest = false;
                is_going_to_last_position_found_grass = true;
            }
        }
        else if (is_going_to_last_position_found_grass)
        {
            if (go_back_to_last_position_found_grass())
                is_going_to_last_position_found_grass = false;
        }
        else if (prey(4.0))
        {
            if (get_hp() > 100.0)
            {
                set_hp(100.0);
                last_position_found_grass = get_position();
                is_carrying_grass = is_going_back_to_nest = true;
            }
        }
        else
            random_motion(4.0);
        if (eco_system->get_environment_instance()->get_rainfall()/3500.0 > EcoSystem::random_double() )
            set_dead();
    }
}

bool Ant::go_back_to_nest()
{
    if (queen == NULL)
    {
        brownian_motion(4.0);
        return true;
    }
    Vector2D tmp = queen->get_position() - get_position();
    if (tmp.modulus() < 10.0)
        return true;
    Vector2D displacement = tmp * (4.0 / tmp.modulus());
    set_position(get_position() + displacement);
    return false;
}

bool Ant::go_back_to_last_position_found_grass()
{
    if (queen == NULL)
    {
        brownian_motion(4.0);
        return true;
    }
    Vector2D tmp = last_position_found_grass - get_position();
    if (tmp.modulus() < 8.0)
        return true;
    Vector2D displacement = tmp * (4.0 / tmp.modulus());
    set_position(get_position() + displacement);
    return false;
}
